﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;
using System.Data.OleDb;
using System.IO;

public partial class Back_FileAdd : System.Web.UI.Page
{
    #region Field
    protected string strConnection = spbtzb.ConString.ConnString;
    private Boolean session_Logined
    {
        set
        {
            Session["Logined"] = value;
        }
        get
        {
            return Session["Logined"] == null ? false : Convert.ToBoolean(Session["Logined"]);
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (session_Logined == false)
            {
                Response.Redirect("Login.aspx?ErrorMessage=" + "您未登录，请在此登录");
            }
        }

    }

    private string GetFileNameByPath(string Path)
    {
        return Path.Substring(Path.LastIndexOf("/") + 1, Path.Length - Path.LastIndexOf("/") - 1);
    }

    //上传图像
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        UpFile();
    }

    private void UpFile()
    {
        string FilePath = Server.MapPath("~/filesUpload");
        if (!System.IO.Directory.Exists(FilePath)) System.IO.Directory.CreateDirectory(FilePath); ;
        HttpFileCollection HFC = Request.Files;

        for (int i = 0; i < HFC.Count; i++)
        {
            HttpPostedFile HF = HFC[i];       
            if (HFC[i].ContentLength > 0)
            {
                OleDbConnection conn = new OleDbConnection(strConnection);
                conn.Open();
                OleDbTransaction trans = conn.BeginTransaction();
                try
                {
                    String strTitle = Path.GetFileName(HF.FileName);
                    String newFileName = System.Guid.NewGuid().ToString().ToLower() + strTitle;

                    string sqlCmd = String.Format("insert into FileList (Title, Type, IsDisplay, FileUrl) "
                             + "values ('{0}', '{1}', true, '{2}' )", strTitle,
                               System.IO.Path.GetExtension(HF.FileName), newFileName);
                    int iRet = AccessHelper.ExecuteNonQuery(trans, System.Data.CommandType.Text, sqlCmd);
                    HF.SaveAs(FilePath + "//" + newFileName);
                    trans.Commit();                   
                    Page.RegisterClientScriptBlock("tip", "<script>alert('上传成功！')</script>");
                }
                catch
                {
                    trans.Rollback();
                    Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "文件上传失败，请重新操作或请联系管理员!");                   
                 //   Page.RegisterClientScriptBlock("tip", "<script>alert('对不起，上传失败，请重新操作或联系管理员！')</script>");
                }
                finally
                {
                    conn.Close();

                }

            }                             
            else
            {
                Page.RegisterClientScriptBlock("tip", "<script>alert('对不起，请选择要上传的文件！')</script>");
            }
        } // for 
    }


}// cs
